package com.fingard.dsp.bank.directbank.cmbc01;

import com.fingard.dsp.bank.directbank.bankInterface.IInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.ReqQryInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.RetAtsInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.RetAtsInCustodyInfoDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

/**
 * 获取在押车辆信息
 *
 * @author marlon
 * @date 2019/1/15
 */
@Component("CMBC01QryInCustodyInfo")
public class CMBC01QryInCustodyInfo extends CMBC01Base implements IInCustodyInfo {
    @Override
    public RetAtsInCustodyInfo qryInCustodyInfo(ReqQryInCustodyInfo qryInCustodyInfo) throws Exception {
        RetAtsInCustodyInfo retAts = new RetAtsInCustodyInfo(qryInCustodyInfo.reqHeader);
        retAts.writeStart();
        try {
            CMBC01Header bankHeader = new CMBC01Header();
            bankHeader.trnCode = "XR210006";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            XElement tmpXElement = new XElement("xDataBody", "");
            //特约店代码
            tmpXElement.addChild(new XElement("dealerCode", qryInCustodyInfo.DealerCode));
            //车辆状态  I：在库 O：已赎
            tmpXElement.addChild(new XElement("vehicleStatus", "I"));
            //入库起始日
            tmpXElement.addChild(new XElement("inStartDate", ""));
            //入库截止日
            tmpXElement.addChild(new XElement("inEndDate", ""));
            //出库起始日
            tmpXElement.addChild(new XElement("outStartDate", ""));
            //出库截止日
            tmpXElement.addChild(new XElement("outEndDate", ""));
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();//写入报文完成

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                    String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                    if ("0".equals(tmpRetCode)) {
                        retAts.writeRespCode("0");
                        retAts.writeRespInfo(tmpRetMessage);
                        while (tmpBkReader.read("/CMBC/xDataBody/Detail")) {
                            RetAtsInCustodyInfoDetail tmpRetDetail = new RetAtsInCustodyInfoDetail();
                            tmpRetDetail.DealerCode = qryInCustodyInfo.DealerCode;
                            tmpRetDetail.VIN = tmpBkReader.readedXElement.getSingleText("vin");
                            tmpRetDetail.PickupPrice = tmpBkReader.readedXElement.getSingleText("price");
                            tmpRetDetail.CarMortgageTime = tmpBkReader.readedXElement.getSingleText("inDate");
                            tmpRetDetail.CarEneDate = tmpBkReader.readedXElement.getSingleText("matureDate");
                            tmpRetDetail.BillNo = tmpBkReader.readedXElement.getSingleText("financeNo");
                            tmpRetDetail.MainRepositoryLocation = tmpBkReader.readedXElement.getSingleText("storageAddress");
                            tmpRetDetail.SecondRepositoryLocation = tmpBkReader.readedXElement.getSingleText("");
                            tmpRetDetail.SecondNetLocation = tmpBkReader.readedXElement.getSingleText("");
                            tmpRetDetail.Reserve1 = "";
                            tmpRetDetail.Reserve2 = "";
                            tmpRetDetail.Reserve3 = "";
                            tmpRetDetail.Reserve4 = "";
                            tmpRetDetail.Reserve5 = "";
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpRetMessage);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
        }
        retAts.writeEnd();
        return retAts;

    }
}



